



import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


public class GuiDesigne 
{
	public GuiDesigne()
	{
		
	}
	
	
	private void drowDesigne()
	{
		
		// the main window layout
		Display display = new Display ();
		Shell shell = new Shell (display);
		GridLayout layout =new GridLayout(1,true);
		layout.marginLeft=10;
		layout.marginRight=10;
		layout.marginTop=7;
		shell.setLayout(layout);
		
		
		//the Menu bar
		 Menu menu = new Menu(shell, SWT.BAR);
		 MenuItem fileItem = new MenuItem(menu, SWT.CASCADE);
		 fileItem.setText("File");
		 MenuItem editItem = new MenuItem(menu, SWT.CASCADE);
		 editItem.setText("Edit");
		 MenuItem viewItem = new MenuItem(menu, SWT.CASCADE);
		 viewItem.setText("View");
		 MenuItem helpItem = new MenuItem(menu, SWT.CASCADE);
		 helpItem.setText("Help");
		 shell.setMenuBar(menu);
		
		 
		//set fileItem Menu
		Menu fileMenu = new Menu(menu);
		fileItem.setMenu(fileMenu);
		
		MenuItem exitItem = new MenuItem(fileMenu, SWT.CASCADE);
		exitItem.setText("Exit");
		
		
		
		
         
         
		
		//the answer label
		 GridData data = new GridData(GridData.FILL_HORIZONTAL);
		 Label answer =new Label(shell,SWT.BORDER);
		 data.heightHint=40;
		 data.widthHint=500;
		 answer.setLayoutData(data);
		 
		 
		 //the text area to write the problem and to display the answer
		 data=new GridData(GridData.FILL_HORIZONTAL);
		 final Text writeProblems =new Text(shell,SWT.BORDER |SWT.WRAP|SWT.MULTI|SWT.V_SCROLL );
		 data.heightHint=150;
		 data.widthHint=500;
		 writeProblems.setLayoutData(data);

		 
		 
		 //the composite to hold the buttons of the calculator
		 Composite functions= new Composite(shell, SWT.BORDER);
		 data =new GridData(GridData.FILL_BOTH);
		 data.heightHint=300;
		 data.widthHint=550;
		 functions.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns = 2;
		 functions.setLayout(layout);
		 
		 
		 //the left composite to hold other 2 composite one of them to hold the numbers and the other for the base operations
		 Composite leftComp= new Composite(functions, SWT.None);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=140;
		 data.widthHint=300;
		 leftComp.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=1;
		 layout.horizontalSpacing=20;
		 leftComp.setLayout(layout);
		 
		//composite to hold the buttons of the numbers
		 Composite numbers = new Composite(leftComp,SWT.BORDER);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=130;
		 data.widthHint=205;
		 numbers.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns = 3;
		 layout.horizontalSpacing=20;
		 numbers.setLayout(layout);
		 
		 //create buttons of numbers		
		 data =new GridData(GridData.FILL_BOTH);
		 data.heightHint=35;
		 data.widthHint=20;
		
		
		 String[] buttonsText = new String[]{"1","2","3","4","5","6","7","8","9","0",".","=","Ans","clear","AC"};
		 for(int i=0;i<buttonsText.length;i++)
		 {
			 final Button numberButton =new Button(numbers,SWT.None);
			 numberButton.setLayoutData(data);
			 numberButton.setText(buttonsText[i]);
			 numberButton.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {
			        
			      }
			    });
		 }
		
		 
		 
		//composite to hold the buttons of the baseOperation
		 Composite baseOperations = new Composite(leftComp,SWT.None);
		 data = new GridData(GridData.FILL_BOTH);
		 baseOperations.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns = 1;
		 baseOperations.setLayout(layout);
		 
		 // create buttons of base operations
		 data =new GridData(GridData.FILL_BOTH);
		 data.heightHint=20;
		 data.widthHint=20;
		 String[] operationsText = new String[]{"Add","Subtraction","Multiply","Division"};
		 for(int i=0;i<operationsText.length;i++)
		 {
			 final Button baseOperationButton =new Button(baseOperations,SWT.None);
			 baseOperationButton.setLayoutData(data);
			 baseOperationButton.setText(operationsText[i]);
			 baseOperationButton.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {
			        //put the type of the text as 1 to but the sign related to it's operation
			    	  String labelText =writeProblems.getText();
			    	  String additionText = baseOperationButton.getText()+"1";
			    	  writeProblems.setText(writeOnLabel(labelText, additionText));
			      }
			    });
			 
		 }
		 
		
		 //right composite to hold the different operations (three other composite)
		 Composite rightComp= new Composite(functions, SWT.None);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=140;
		 data.widthHint=400;
		 rightComp.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=1;
		 rightComp.setLayout(layout);
		 
		 
		 
		 //first composite for Trigonometric functions
		 Composite trigonomerticFunctions= new Composite(rightComp, SWT.BORDER);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=85;
		 data.widthHint=305;
		 trigonomerticFunctions.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=6;
		 layout.verticalSpacing=10;
		 trigonomerticFunctions.setLayout(layout);
		 
		 // create the buttons
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=30;
		 data.widthHint=45;
		 String[]trigonometricText =new String[]{"Sin","Cos","Tan","Sec","Cosec","cot","aSin","aCos","aTan","Sinh","Cosh","Tanh"};
		 for(int i=0;i<trigonometricText.length;i++)
		 {
			 final Button trigonometricFun = new Button( trigonomerticFunctions,SWT.None);
			 trigonometricFun.setLayoutData(data);
			 trigonometricFun.setText(trigonometricText[i]);
			 trigonometricFun.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {
			        String labelText = writeProblems.getText();
			        String additionText = trigonometricFun.getText()+"2";
			        writeProblems.setText(writeOnLabel(labelText, additionText));			    	 
			      }
			    });
		 }
		 
		 
		 
		 
		 //second composite for other functions
		 Composite GeneralFunctions= new Composite(rightComp, SWT.BORDER);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=140;
		 data.widthHint=305;
		 GeneralFunctions.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=6;
		 layout.verticalSpacing=10;
		 GeneralFunctions.setLayout(layout);
		 
		 
		 //create the buttons
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=30;
		 data.widthHint=45;
		 String[] GeneralFunctionsText =new String[]{"(",")","Abs","Mod","%","1/x","e","ln","log"};
		 for(int i=0;i<GeneralFunctionsText.length;i++)
		 {
			 final Button GeneralFun = new Button(GeneralFunctions,SWT.None);
			 GeneralFun.setLayoutData(data);
			 GeneralFun.setText(GeneralFunctionsText[i]);
			 GeneralFun.addSelectionListener(new SelectionAdapter() {
			      public void widgetSelected(SelectionEvent event) {
			        String labelText = writeProblems.getText();
			        String additionText = GeneralFun.getText();
			        if(additionText.equalsIgnoreCase("Abs")|| additionText.equalsIgnoreCase("ln")|| additionText.equalsIgnoreCase("log"))
			        {
			        	additionText = GeneralFun.getText()+"2";
			        }
			        else if(additionText.equalsIgnoreCase("%"))
			        {
			        	additionText = GeneralFun.getText()+"3";
			        }
			        else if(additionText.equalsIgnoreCase("1/x"))
			        {
			        	additionText = GeneralFun.getText()+"4";
			        }
			        else if(additionText.equalsIgnoreCase("Mod"))
			        {
			        	additionText = GeneralFun.getText()+"5";
			        }
			        else if(additionText.equalsIgnoreCase("e"))
			        {
			        	additionText = GeneralFun.getText()+"6";
			        }
			        writeProblems.setText(writeOnLabel(labelText, additionText));
			      }
			    });
			
		 }
		 
		 
		 //other buttons but need to be an image not text
		 Image[]images =new Image[8];
		 images[0]=new Image(display,"signs/power2.png");
		 images[1]=new Image(display,"signs/power3.png");
		 images[2]=new Image(display,"signs/power.png");
		 images[3]=new Image(display,"signs/10powerx.png");
		 images[4]=new Image(display,"signs/root.png");
		 images[5]=new Image(display,"signs/root2.png");
		 images[6]=new Image(display,"signs/root3.png");
		 images[7]=new Image(display,"signs/logx(y).png");
		 
		 
		//create the other buttons
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=30;
		 data.widthHint=45;
		 for(int i=0 ;i<images.length;i++)
		 {
			 Button ImageButton =new Button(GeneralFunctions,SWT.None);
			 ImageButton.setLayoutData(data);
			 ImageButton.setImage(images[i]);
		 }
		 
		
		 
		 //third composite for the constant part
		 Composite constantPart= new Composite(rightComp, SWT.BORDER);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=50;
		 data.widthHint=305;
		 constantPart.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=2;
		 constantPart.setLayout(layout);
		 
		 //the constant button
		 Button constant =new Button(constantPart,SWT.None);
		 data = new GridData(GridData.FILL_BOTH);
		 data.heightHint=35;
		 data.widthHint=100;
		 constant.setLayoutData(data);
		 constant.setText("Constants");
		 
		 //composite to hold the type of the operations (Degree || radian)
		 Composite typeOfOperations= new Composite(constantPart, SWT.BORDER);
		 data = new GridData(GridData.FILL_BOTH);
		 typeOfOperations.setLayoutData(data);
		 layout = new GridLayout();
		 layout.numColumns=2;
		 layout.horizontalSpacing=10;
		 typeOfOperations.setLayout(layout);
		 
		 
		 //Buttons (Degree &radian)
		 data = new GridData(GridData.FILL_BOTH);
		 Button degree =new Button(typeOfOperations,SWT.RADIO);
		 degree.setText("Degree");
		 degree.setLayoutData(data);
		 
		 Button radian =new Button(typeOfOperations,SWT.RADIO);
		 radian.setText("Radian");
		 radian.setLayoutData(data);
		
		
		shell.pack ();
		shell.open ();
        
		//closing the display
		
		while (!shell.isDisposed ()) {
			if (!display.readAndDispatch ())
				display.sleep ();
		}
		display.dispose ();
	}
	
	
	
	public void drow()
	{
		drowDesigne();
	}
	
	//take care that additionText have a key related to the type of the text and what should i do with it
	private String writeOnLabel(String labelText , String additionText)
	{
		String requiredText = "";
		int typeOfText = Integer.parseInt(""+additionText.charAt(additionText.length()-1));
	    additionText = additionText.substring(0,additionText.length()-1);
		switch(typeOfText)
		{
		//to handle the base operations
		case 1: if(additionText.equalsIgnoreCase("add")) requiredText=labelText+"+";
		        else if(additionText.equalsIgnoreCase("Subtraction")) requiredText=labelText+"-";
		        else if(additionText.equalsIgnoreCase("Multiply")) requiredText=labelText+"*";
		        else requiredText=labelText+"/";                                                       break;
		    	//to handle the trigonometric functions & ln & log & Abs         
		case 2: if(labelText.equalsIgnoreCase(""))requiredText=labelText+additionText+"(";
		        else  requiredText=labelText+additionText+"(";                                         break;
		//to handle  % sign
		case 3: if(!labelText.equalsIgnoreCase(""))requiredText="("+labelText+")"+additionText;        break;
		//to handle 1/x sign
		case 4: if(!labelText.equalsIgnoreCase(""))requiredText="1/("+labelText+")";
		        else requiredText="1/(";                                                               break;
		//to handle mod operation
		case 5: if(!labelText.equalsIgnoreCase(""))requiredText="("+labelText+")"+additionText+"(";     break;
		//to handle e sign
		case 6: if(labelText.equalsIgnoreCase(""))requiredText="e^("; 
		        else requiredText=labelText+"e^(";                                                    break;
		
		}
		return requiredText;
	}
}
